package com.hust.hui.quick.service.dao;

import com.hust.hui.quick.service.domain.DirEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * Created by yihui on 2017/7/16.
 */
@Mapper
public interface DirMapper {


    @Update("CREATE TABLE `dir` (\n" +
            "  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,\n" +
            "  `name` varchar(20) NOT NULL DEFAULT '',\n" +
            "  `parentId` int(11) NOT NULL DEFAULT '0',\n" +
            "  `level` int(4) NOT NULL DEFAULT '0',\n" +
            "  `userId` int(11) NOT NULL DEFAULT '0',\n" +
            "  `isDeleted` tinyint(1) NOT NULL DEFAULT '0',\n" +
            "  `created` int(13) NOT NULL DEFAULT '0',\n" +
            "  `updated` int(13) NOT NULL DEFAULT '0',\n" +
            "  PRIMARY KEY (`id`),\n" +
            "  index `idx_userId` (`userId`),\n" +
            "  index `idx_parentId_name` (`parentId`, `name`)\n" +
            ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;")
    void create();


    /**
     * 新增一条目录记录
     *
     * @param dirEntity
     * @return
     */
    @Insert("INSERT INTO dir(name,parentId,level,userId,isDeleted,created,updated) " +
            "VALUES(#{name}, #{parentId}, #{level},#{userId}," +
            "0, unix_timestamp(now()), unix_timestamp(now()))")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(DirEntity dirEntity);


    /**
     * 查询服务， 根据用户id查询所有的目录
     * 根据 id 查询目录信息
     * 根据 parentId 查询目录的所有子目录
     *
     * @param map
     * @return
     */
    @Select("<script>" +
            "select * from dir " +
            "<where>" +
            "<if test='userId !=null'> userId=#{userId} </if>" +
            "<if test='id !=null'> and id=#{id} </if>" +
            "<if test='parentId !=null'> and parentId=#{parentId}</if>" +
            "<if test='name !=null'> and name=#{name}</if>" +
            "<if test='level !=null'> and level=#{level}</if>" +
            "<if test='isDeleted!=null'> and isDeleted=#{isDeleted}</if>" +
            "</where>" +
            " limit 100" +
            "</script>")
    List<DirEntity> query(Map<String, Object> map);


    /**
     * 目录更新， 主要是目录的移动 + 重命名两种操作
     *
     * @param params
     */
    @Update("<script>" +
            "update dir " +
            "<set>" +
            "<if test='name!=null'> name=#{name},</if>" +
            "<if test='parentId!=null'> parentId=#{parentId},</if>" +
            "<if test='level!=null'> level=#{level},</if>" +
            "updated=unix_timestamp(now())" +
            "</set>" +
            "<where>" +
            "id=#{id}" +
            "</where>" +
            "</script>")
    int update(Map<String, Object> params);


    /**
     * 逻辑删除目录
     *
     * @param ids
     * @return
     */
    @Update("<script>" +
            "update dir set isDeleted=1 " +
            "where " +
            " id in " +
            "<foreach collection=\"ids\" index=\"index\" item=\"id\" open=\"(\" separator=\",\" close=\")\">" +
            "   #{id}" +
            "</foreach> " +
            "" +
            "</script>")
    int delete(@Param("ids") List<Long> ids);

}
